const homework = require('../models/homework')
const users = require('../models/userDetail')

var express = require('express')
var router = express.Router()

var formidable = require('formidable')

router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
        if(err) {
            res.json({'state': false, 'msg': err})
        }else {
            let find_num = () => {
                return new Promise(function (reslove, reject) {
                    homework.find({}, (err, docs) => {
                        if(err) {
                            res.json({'state': false, 'msg': err});
                        } else {
                            reslove(docs.length+1);
                        }
                    })
                })
            }

            let return_data = (num) => {
                users.find({
                    flags: {
                        $all: fields.flags
                    }
                }, (err, stus) => {
                    if(err) {
                        res.json({'state': false, 'msg': err})
                    }else {
                        let last_time = new Date(new Date(fields.time) + (8*60*60*1000));
                        console.log(last_time);
                        var formatDate = function (date) {
                            var y = date.getFullYear();
                            var m = date.getMonth() + 1;
                            m = m < 10 ? ('0' + m) : m;
                            var d = date.getDate();
                            d = d < 10 ? ('0' + d) : d;
                            var h = date.getHours();
                            h=h < 10 ? ('0' + h) : h;
                            var minute = date.getMinutes();
                            minute = minute < 10 ? ('0' + minute) : minute;
                            var second=date.getSeconds();
                            second=second < 10 ? ('0' + second) : second;
                            return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
                        }
                        last_time = formatDate(last_time)
                        homework.create({
                            flags: fields.flags,
                            UUID: fields.UUID,
                            seq: num,
                            name: fields.name,
                            need: fields.need,
                            type: fields.type,
                            question: fields.question,
                            stu_num: stus.length,
                            submit_num: 0,
                            time: last_time,
                            is_late: fields.is_late
                        }, (err, doc) => {
                            if(err) {
                                res.json({'state': false, 'msg': err});
                            } else {
                                res.json({
                                    'state': true, 
                                    'msg': '作业发布成功！', 
                                    'homework_num': num, 
                                    'stu_num': stus.length
                                });
                            }
                        })
                    }
                })
            }

            find_num().then(return_data)
        }
        
    });
});

module.exports = router;